<?php
/**
 * spsNewsletter controller for sending newsletters
 * 
 * @author Satyan Shah <joomlaDesign@modernbridge.com>
 * @link http://www.modernbridge.com
 * @license GNU/GPL
 */


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

class spsNewslettersControllersendNewsletter extends spsNewslettersController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
	}

	function send_test()
	{
   		// extract body for message and validate
		$body = JRequest::getVar( 'body','', 'post', 'string',JREQUEST_ALLOWRAW);
		if (!$body) 
		{
			$msg = JText::_( "The body is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <-Exit Point. Task Failed.
		}
		 
		// extract from name and validate
		$fromName = JRequest::getVar( 'fromName','', 'post', 'string');
		if (!$fromName) 
		{
			$msg = JText::_( "The from name is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <-Exit Point. Task Failed.
		}
    
		// extract from email and validate
		$fromEmail = JRequest::getVar( 'fromEmail','', 'post', 'string');
		if (!$fromEmail) 
		{
			$msg = JText::_( "The from email address is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <-Exit Point. Task Failed.
		}
		
		// get the model, we need to get the test email address
		$nM =& $this->getModel('sendNewsletter');
		$params=$nM->getComponentParameters();
		$testEmailAddress = $params->get('testEmailAddress');  
		
		// validate test email
		if (!$testEmailAddress) 
		{
			$msg = JText::_( "The test email address is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <- Exit Point. Task Failed.
		}
		
		// extract subject and validate
		$subject = JRequest::getVar( 'subject','', 'post', 'string');
		if (!$subject) 
		{
			$msg = JText::_( "The subject is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <- Exit Point. Task Failed.
		}
		
		// all fields validated, send mail.
		$sendEmailStatus=JUtility::sendMail($fromEmail, $fromName, $testEmailAddress, $subject, $body,1);
		// set user message and redirect back.
		$msg = JText::_( "Test Message Sent to $testEmailAddress"); 
		$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
		$this->setRedirect($link, $msg);
    
		return true; // <-Exit Point. Task Successful.

	}

	function send_all()
	{
	
		// extract body for message and validate
		$body = JRequest::getVar( 'body','', 'post', 'string',JREQUEST_ALLOWRAW);
		if (!$body) 
		{
			$msg = JText::_( "The body is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <-Exit Point. Task Failed.
		}
		 
		// extract from name and validate
		$fromName = JRequest::getVar( 'fromName','', 'post', 'string');
		if (!$fromName) 
		{
			$msg = JText::_( "The from name is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <-Exit Point. Task Failed.
		}
    
		// extract from email and validate
		$fromEmail = JRequest::getVar( 'fromEmail','', 'post', 'string');
		if (!$fromEmail) 
		{
			$msg = JText::_( "The from email address is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <-Exit Point. Task Failed.
		}

    
		// get the model, we need to get the test email address
		$nM =& $this->getModel('sendNewsletter');
		$params=$nM->getComponentParameters();
		$testEmailAddress = $params->get('testEmailAddress');  
		
		// validate test email
		if (!$testEmailAddress) 
		{
			$msg = JText::_( "The test email address is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <- Exit Point. Task Failed.
		}
		
		// extract subject and validate
		$subject = JRequest::getVar( 'subject','', 'post', 'string');
		if (!$subject) 
		{
			$msg = JText::_( "The subject is empty! Send Message routine aborted."); 
			$link = 'index.php?option=com_spsNewsletter&controller=sendNewsletter';
			$this->setRedirect($link, $msg);
			return false; // <- Exit Point. Task Failed.
		}

  
		//all fields have been valiated, send the test email as a copy
		$testSubject="Copy of: ".$subject;
		$sendEmailStatus=JUtility::sendMail($fromEmail, $fromName, $testEmailAddress, $testSubject, $body,1);
			
		// get model and then records to send mass mailing
		$subscribersModel = $this->getModel('subscribers'); 
		$subscribers = $subscribersModel->getData();
		// total up number of records to send
		$n = count( $subscribers );
		// instantiate the sendNewsletter model, we will need to use convienence function that is built in to place the unsubscribe link
		$newsletterModel =$this->getModel('sendNewsletter');
    
		for ($i = 0; $i < $n; $i++)
		{
			// use the convienence function to create the unsubscribe link
			$convertedBody=$newsletterModel->convertUnsubscribeLink($body,$subscribers[$i]->id);
			// send the email
			$sendEmailStatus=JUtility::sendMail($fromEmail, $fromName, $subscribers[$i]->email, $subject, $convertedBody,1);
		}
	
		// set user message and redirect.
		$msg = JText::_( "Message Sent to $n subscribers."); 
		$link = 'index.php?option=com_spsnewsletter&controller=sendNewsletter';
		$this->setRedirect($link, $msg);
   
		return true; // <-Exit Point. Task Successful.

	}

}
?>
